import {Base} from "../../utils/base.js";
class Song extends Base{
  constructor(){
    super();
  }

  //获取歌曲的歌词
  getLrc(url,callback){
    var that=this;
    wx.request({
      url:url,
      method:"get",
      success:function(res){
        var lrc=that.dealLrc(res.data,that);
        callback&&callback(lrc);
      },
      fail:function(res){
        console.log(res);
      }
    });
  }

  //处理歌词
  dealLrc(lrc,that) {
    var arr = lrc.split("\n");
    var timeArr = [], lrcArr = [];
    arr.forEach(function (item, index) {
      var time = item.slice(item.indexOf("["), item.indexOf("]") + 1);
      time = that.dealLrcTime(time);
      timeArr.push(time);
      var lrc = item.substr(item.indexOf("]") + 1) + "\n";
      lrcArr.push(lrc);
    });
    return {
      lrcArr:lrcArr,
      timeArr:timeArr
    };
  }

  //处理歌词时间
  dealLrcTime(time) {
    time = time.replace("[", "");
    time = time.replace("]", "");
    var arr = time.split(":");
    var relTime = parseInt(arr[0]) * 60 + parseInt(arr[1]);
    return relTime;
  }

  //找到当前播放时间
  findCurrentIndex(currentTime,timeArr) {
    var index = -1;
    for (let i = 0; i < timeArr.length; ++i) {
      if (timeArr[i] == NaN) {
        continue;
      }
      if (currentTime <= timeArr[i]) {
        index = i;
        break;
      }
    }
    return index;
  }

}

export {Song};